package rps.game;

import java.rmi.RemoteException;


import rps.game.data.Figure;
import rps.game.data.FigureKind;
import rps.game.data.Move;
import rps.game.data.Player;

public class ValidatingGame implements Game {

	private final Game game;
	private Player player;
	private boolean assignmentFlag = false;
	private boolean assignmentTrap = false;

	public ValidatingGame(Game game, Player player) throws RemoteException {
		this.game = game;
		this.player = player;
		
	}

	@Override
	public void setInitialAssignment(Player p, FigureKind[] assignment) throws RemoteException {
		if(!p.equals(player)) throw new IllegalArgumentException("Player is not allowed to submit Assignment");
		for (int i = 0; i < assignment.length; i++) {
			if((i < 28) && assignment[i] != null) throw new IllegalArgumentException();
			if((i > 28) && assignment[i] == null) throw new IllegalArgumentException();
			if(assignment[i] == FigureKind.TRAP)
				if(assignmentTrap) throw new IllegalArgumentException("Assignment contains more than one Trap");
				else assignmentTrap = true;
			if(assignment[i] == FigureKind.FLAG)
				if(assignmentFlag) throw new IllegalArgumentException("Assignment contains more than one Flag");
				else assignmentFlag = true;	
		}
		if(assignmentTrap == false) throw new IllegalArgumentException("Assignment contains no Trap");
		if(assignmentFlag == false) throw new IllegalArgumentException("Assignment contains no Flag");
		game.setInitialAssignment(p, assignment);
	}

	@Override
	public Figure[] getField() throws RemoteException {
		return game.getField();
	}

	@Override
	public void move(Player p, int from, int to) throws RemoteException {
		
		Figure[] board = game.getField();
		int mod = from % 7;
		
		
		if(to < 0) throw new IllegalArgumentException("Moving to negative Positions is not allowed");
		if(from == to) throw new IllegalArgumentException("Moving to the same Coordinate is an Invalid Move!");
		
		else if(mod == 6){
			if(!(to == from + 7 || to == from -7 || to == from - 1)) throw new IllegalArgumentException();
		}
		else if(mod == 0){
			if(!(to == from + 7 || to == from -7 || to == from + 1)) 
				throw new IllegalArgumentException();
		}
		else if(!(to == from + 7 || to == from -7 || to == from - 1 || to == from + 1)) 
			throw new IllegalArgumentException();
		
		
		if(!player.equals(p)) throw new IllegalArgumentException("This Player is not allowed to move");
		if(!board[from].belongsTo(p)) throw new IllegalArgumentException("Player is not allowed to move Opponents Figures");
		if(board[to] != null && board[to].belongsTo(p)) throw new IllegalArgumentException("Player is not allowed to attack its own Figures");
		if(board[from].getKind() == FigureKind.TRAP || board[from].getKind() == FigureKind.FLAG) throw new IllegalArgumentException("Player is not allowed to move Flag or Trap");
		
		else game.move(p, from, to);
	}

	@Override
	public Move getLastMove() throws RemoteException {
		return game.getLastMove();
	}

	@Override
	public void sendMessage(Player p, String message) throws RemoteException {
		game.sendMessage(p, message);
	}

	@Override
	public void setInitialChoice(Player p, FigureKind kind) throws RemoteException {
		if(kind == FigureKind.FLAG || kind == FigureKind.TRAP || kind == FigureKind.HIDDEN) throw new IllegalArgumentException("Setting Flag, Trap or HiddenType as InitialChoice is not allowed!");		
		else game.setInitialChoice(p, kind);
	}

	@Override
	public void setUpdatedKindAfterDraw(Player p, FigureKind kind) throws RemoteException {
		if(kind == FigureKind.FLAG || kind == FigureKind.TRAP || kind == FigureKind.HIDDEN) throw new IllegalArgumentException("Setting Flag, Trap or HiddenType as ChoiceAfterDraw is not allowed!");
		game.setUpdatedKindAfterDraw(p, kind);
	}

	@Override
	public void surrender(Player p) throws RemoteException {
		game.surrender(p);
	}

	@Override
	public Player getOpponent(Player p) throws RemoteException {
		return game.getOpponent(p);
	}
}